home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / UI / memory.tk < prev    next >
Text File  |  1995-07-26  |  14KB  |  349 lines

  1. ###############################################################################
  2. # $Id: memory.tk,v 1.6 1995/02/18 18:00:59 bmott Exp $
  3. ###############################################################################
  4. # memory.tk - Routines to handle the memory viewing screen
  5. #
  6. # Copyright 1993 
  7. # Bradford W. Mott
  8. # November 11,1993
  9. ###############################################################################
  10. # $Log: memory.tk,v $
  11. # Revision 1.6  1995/02/18  18:00:59  bmott
  12. # Added routine to catch bad filenames in the dump utility
  13. #
  14. # Revision 1.5  1995/02/11  03:29:30  bmott
  15. # Fixed bug in dump memory to file utility
  16. #
  17. # Revision 1.4  1995/01/01  03:01:13  bmott
  18. # Added bitmaps to buttons, changed method to alter memory locations,
  19. # and added a dismiss button
  20. #
  21. # Revision 1.3  1994/09/13  23:29:01  bmott
  22. # Changed the file selector calls to use BtkFileSelector
  23. #
  24. # Revision 1.2  1994/08/22  08:25:03  bmott
  25. # Changed the dump dialog to popup in the memory viewer window
  26. #
  27. # Revision 1.1  1994/02/18  20:29:40  bmott
  28. # Initial revision
  29. #
  30. ###############################################################################
  31.  
  32.  
  33. ###############################################################################
  34. # Check to make sure the the address is within range
  35. ###############################################################################
  36. proc CheckMemoryViewerAddress {} {
  37.   global MemoryViewer
  38.  
  39.   if {[expr $MemoryViewer(Address) + $MemoryViewer(WPS) - 1]  > \
  40.       $MemoryViewer(MaximumAddress)} {
  41.     set MemoryViewer(Address) [expr $MemoryViewer(MaximumAddress) - \
  42.         $MemoryViewer(WPS) + 1]
  43.   }
  44.   if {$MemoryViewer(Address) < "0"} {
  45.     set MemoryViewer(Address) 0
  46.   }
  47. }
  48.  
  49. ###############################################################################
  50. # Refresh the memory viewer text window
  51. ###############################################################################
  52. proc RefreshMemoryViewer {} {
  53.   global MemoryViewer
  54.  
  55.   ## Make sure the memory viewer exists
  56.   if {[winfo exists .memoryViewer]==0} {return}
  57.  
  58.   ## Get the length, address, and words per line in hexidecimal form
  59.   set length [format "%x" $MemoryViewer(WPS)]
  60.   set address [format "%x" $MemoryViewer(Address)]
  61.   set wpl [format "%x" $MemoryViewer(WPL)]
  62.  
  63.   ## Ask the simulator for the memory dump
  64.   PutLine "ListMemory $MemoryViewer(AddressSpace) $address $length $wpl"
  65.  
  66.   ## Empty the text widget
  67.   .memoryViewer.text delete 1.0 end
  68.  
  69.   ## Put the memory dump in the text widget
  70.   set address [expr $MemoryViewer(Address)]
  71.   foreach i [GetList] {
  72.     set line "[format %0$MemoryViewer(AddressLength)x $address]: $i\n"
  73.     .memoryViewer.text insert end "$line"
  74.     set address [expr $address+$MemoryViewer(WPL)]
  75.   }
  76. }
  77.  
  78. ###############################################################################
  79. # Change the address the viewer is displaying
  80. ###############################################################################
  81. proc ChangeMemoryViewerAddress {} {
  82.   global MemoryViewer
  83.  
  84.   ## Get the value from the user
  85.   set value [ChildEntryDialog .memoryViewer "Enter new address:"\
  86.                  {^[0-9a-fA-F]+$}]
  87.  
  88.   ## If Okay was press then change the address
  89.   if {$value!=""} {
  90.     scan $value "%x" MemoryViewer(Address)
  91.     CheckMemoryViewerAddress
  92.     RefreshMemoryViewer
  93.   }
  94. }
  95.  
  96. ###############################################################################
  97. # Alter the memory location where the mouse button was pressed
  98. ###############################################################################
  99. proc MemoryViewerAlterMemory {x y} {
  100.   global MemoryViewer
  101.  
  102.   if {[.memoryViewer.text get "@$x,$y wordstart" "@$x,$y wordend"] == " "} {
  103.     return
  104.   }
  105.   scan [.memoryViewer.text index "@$x,$y wordend"] "%d.%d" endl endw
  106.   if {$endw > [expr $MemoryViewer(AddressLength)+1]} {
  107.     .memoryViewer.text tag add WordSelectTag "@$x,$y wordstart" "@$x,$y wordend"
  108.   }
  109.  
  110.   ## Get a list of indices of all of the selected words
  111.   set range [.memoryViewer.text tag ranges WordSelectTag]
  112.  
  113.   ## If no word is selected then leave
  114.   if {$range==""} { return }
  115.  
  116.   ## Build a regular expression to check the entry against
  117.   set reg_exp ""
  118.   for {set t 0} {$t < $MemoryViewer(WordLength)} {set t [expr $t+1]} {
  119.     for {set s 0;set add ""} {$s < [expr $t+1]} {set s [expr $s+1]} {
  120.       set add "$add\[0-9a-fA-F\]"
  121.     }
  122.     if {$reg_exp==""} {
  123.       set reg_exp "(^$add\$)"
  124.     } else {
  125.       set reg_exp "$reg_exp|(^$add\$)"
  126.     }
  127.   }
  128.  
  129.   ## Get the value from the user
  130.   set value [ChildEntryDialog .memoryViewer \
  131.                  "Enter new value for the selected memory location(s):" \
  132.                  $reg_exp]
  133.  
  134.   ## If Okay was pressed then we have to set each word to the new value
  135.   if {$value!=""} {
  136.     for {set t 0} {$t<[llength $range]} {set t [expr $t+2]} {
  137.       scan "[lindex $range $t]" "%d.%d" y x
  138.       set address [expr $MemoryViewer(Address)+($y-1)*$MemoryViewer(WPL)+\
  139.                         ($x-$MemoryViewer(AddressLength)-1)/\
  140.                         (1+$MemoryViewer(WordLength))]
  141.       set address [format "%x" $address]
  142.       PutLine "SetMemory $MemoryViewer(AddressSpace) $address $value"
  143.       set dummy [GetList]
  144.     }
  145.   }
  146.   RefreshMemoryViewer
  147. }
  148.  
  149. ###############################################################################
  150. # Dump memory to a file
  151. ###############################################################################
  152. proc DumpMemoryViewer {} {
  153.   global MemoryViewer
  154.   global ReturnValue
  155.  
  156.   ## Just in case destroy the window
  157.   catch {destroy .memoryViewer.dumpDialog}
  158.  
  159.   frame .memoryViewer.dumpDialog -relief raised -borderwidth 3
  160.  
  161.   frame .memoryViewer.dumpDialog.start -relief groove -borderwidth 2 
  162.     label .memoryViewer.dumpDialog.start.label -text "Starting Address:"
  163.     entry .memoryViewer.dumpDialog.start.entry -width 10 -relief sunken
  164.     bind .memoryViewer.dumpDialog.start.entry <Return> {
  165.          focus .memoryViewer.dumpDialog.end.entry
  166.     }
  167.     pack .memoryViewer.dumpDialog.start.label -side left -padx 4
  168.     pack .memoryViewer.dumpDialog.start.entry -side left \
  169.          -fill x -expand 1 -padx 4
  170.  
  171.   frame .memoryViewer.dumpDialog.end -relief groove -borderwidth 2 
  172.     label .memoryViewer.dumpDialog.end.label -text "Ending Address:"
  173.     entry .memoryViewer.dumpDialog.end.entry -width 10 -relief sunken
  174.     bind .memoryViewer.dumpDialog.end.entry <Return> {
  175.          set ReturnValue "[.memoryViewer.dumpDialog.start.entry get] [.memoryViewer.dumpDialog.end.entry get]";
  176.                   destroy .memoryViewer.dumpDialog 
  177.     }
  178.     pack .memoryViewer.dumpDialog.end.label -side left -padx 4
  179.     pack .memoryViewer.dumpDialog.end.entry -side left \
  180.          -fill x -expand 1 -padx 4
  181.  
  182.   frame .memoryViewer.dumpDialog.buttons
  183.     button .memoryViewer.dumpDialog.buttons.ok -text "Okay" \
  184.         -command {set ReturnValue "[.memoryViewer.dumpDialog.start.entry get] [.memoryViewer.dumpDialog.end.entry get]"; \
  185.                   destroy .memoryViewer.dumpDialog }
  186.     button .memoryViewer.dumpDialog.buttons.cancel -text "Cancel" \
  187.         -command {set ReturnValue ""; destroy .memoryViewer.dumpDialog}
  188.     pack .memoryViewer.dumpDialog.buttons.ok -side left -expand 1 -fill x -padx 4
  189.     pack .memoryViewer.dumpDialog.buttons.cancel -side right -expand 1\
  190.           -fill x -padx 4
  191.  
  192.   pack .memoryViewer.dumpDialog.start -side top -fill x -pady 4 -padx 4
  193.   pack .memoryViewer.dumpDialog.end -side top -fill x -pady 4 -padx 4
  194.   pack .memoryViewer.dumpDialog.buttons -side top -fill x -pady 4
  195.  
  196.   ## Place the dump dialog
  197.   place .memoryViewer.dumpDialog -relx 0.5 -rely 0.5 -anchor center
  198.  
  199.   ## Make this a modal dialog
  200.   tkwait visibility .memoryViewer.dumpDialog
  201.   focus .memoryViewer.dumpDialog.start.entry
  202.   grab set .memoryViewer.dumpDialog
  203.   tkwait window .memoryViewer.dumpDialog
  204.  
  205.   # If Okay was pressed set the register value in the simulator
  206.   if {$ReturnValue!=""} {
  207.     if {[regexp {^[0-9a-fA-F]+[ ][0-9a-fA-F]+$} $ReturnValue]==0} {
  208.       ChildAlertDialog .memoryViewer "There was a problem with one of the addresses entered!!!"
  209.       return
  210.     }
  211.     scan "$ReturnValue" "%x %x" start end
  212.     if {($start < 0) || ($end > $MemoryViewer(MaximumAddress)) || \
  213.         ($start > $end)} {
  214.       ChildAlertDialog .memoryViewer "There was a problem with one of the addresses entered!!!"
  215.       return
  216.     }
  217.     
  218.     set name [BtkFileSelector -in .memoryViewer \
  219.          -text "Select file to append memory dump to:"]
  220.     if {$name!=""} {
  221.       if {[file isdirectory $name]} {
  222.         ChildAlertDialog .memoryViewer {ERROR: The name specified is a directory!!!}
  223.         return
  224.       }
  225.  
  226.       ## Calculate the words per line for an 80 column text file
  227.       set WPL [expr (80-1-$MemoryViewer(AddressLength))/\
  228.                     (1+$MemoryViewer(WordLength))]
  229.  
  230.       ## Get the length, address, and WPL in hexidecimal form
  231.       set length [format "%x" [expr $end-$start+1]]
  232.       set address [format "%x" $start]
  233.       set wpl [format "%x" $WPL]
  234.  
  235.       ## Open the output file
  236.       if {[catch {open $name "a+"} file] != 0} {
  237.         ChildAlertDialog .memoryViewer "ERROR: Couldn't open $name for appending!!!"
  238.         return
  239.       } 
  240.  
  241.       ## Ask simulator for memory dump
  242.       PutLine "ListMemory $MemoryViewer(AddressSpace) $address $length $wpl"
  243.  
  244.       ## Output memory dump to file
  245.       set address $start
  246.       foreach i [GetList] {
  247.         set line "[format %0$MemoryViewer(AddressLength)x $address]: $i"
  248.         puts $file $line
  249.         set address [expr $address+$WPL]
  250.       }
  251.       puts $file ""
  252.  
  253.       close $file 
  254.     }
  255.   }
  256.  
  257. ###############################################################################
  258. # Close the memory viewer window
  259. ###############################################################################
  260. proc CloseMemoryViewer {} {
  261.   catch {destroy .memoryViewer}
  262. }
  263.  
  264. ###############################################################################
  265. # Create and display the memory viewer window
  266. ###############################################################################
  267. proc OpenMemoryViewer {} {
  268.   global Program
  269.   global MemoryViewer
  270.  
  271.   ## We start at memory location 0 and address space 0
  272.   set MemoryViewer(Address) 0
  273.   set MemoryViewer(AddressSpace) 0
  274.  
  275.   ## Destroy the window if it exists
  276.   catch {destroy .memoryViewer}
  277.  
  278.   ## Create the top level window
  279.   toplevel .memoryViewer
  280.   wm title .memoryViewer "Memory Viewer"
  281.   wm iconname .memoryViewer "Memory Viewer"
  282.  
  283.   ## Create the text widget to display memory dump in
  284.   text .memoryViewer.text -relief raised -borderwidth 2 -cursor left_ptr
  285.   .memoryViewer.text tag configure WordSelectTag -foreground White \
  286.       -background Black
  287.   bind .memoryViewer.text <Any-KeyPress> "NOP"
  288.   bind .memoryViewer.text <Any-ButtonPress> "NOP"
  289.   bind .memoryViewer.text <Any-Motion> "NOP"
  290.   bind .memoryViewer.text <Button-1> "MemoryViewerAlterMemory %x %y"
  291.  
  292.   ## Create buttons
  293.   button .memoryViewer.dump -text "Dump..." \
  294.       -command DumpMemoryViewer
  295.   button .memoryViewer.address -text "Address..." \
  296.       -command ChangeMemoryViewerAddress
  297.   button .memoryViewer.up -bitmap @$Program(BitmapDir)/Up.xbm \
  298.       -command {set MemoryViewer(Address) [expr $MemoryViewer(Address) - \
  299.           $MemoryViewer(WPL)]; CheckMemoryViewerAddress; RefreshMemoryViewer}
  300.   button .memoryViewer.down -bitmap @$Program(BitmapDir)/Down.xbm \
  301.       -command {set MemoryViewer(Address) [expr $MemoryViewer(Address) + \
  302.           $MemoryViewer(WPL)]; CheckMemoryViewerAddress; RefreshMemoryViewer}
  303.   button .memoryViewer.pageUp -bitmap @$Program(BitmapDir)/PageUp.xbm \
  304.       -command {set MemoryViewer(Address) [expr $MemoryViewer(Address) - \
  305.           $MemoryViewer(WPS)]; CheckMemoryViewerAddress; RefreshMemoryViewer}
  306.   button .memoryViewer.pageDown -bitmap @$Program(BitmapDir)/PageDown.xbm \
  307.       -command {set MemoryViewer(Address) [expr $MemoryViewer(Address) + \
  308.           $MemoryViewer(WPS)]; CheckMemoryViewerAddress; RefreshMemoryViewer}
  309.   button .memoryViewer.dismiss -text "Dismiss" \
  310.       -command {CloseMemoryViewer}
  311.  
  312.   pack .memoryViewer.text -side top -padx 2 -pady 2
  313.   pack .memoryViewer.dump -side left -padx 2 -pady 2 -fill both -expand 1
  314.   pack .memoryViewer.address -side left -padx 2 -pady 2 -fill both -expand 1
  315.   pack .memoryViewer.up -side left -padx 2 -pady 2 -fill both -expand 1
  316.   pack .memoryViewer.down -side left -padx 2 -pady 2 -fill both -expand 1
  317.   pack .memoryViewer.pageUp -side left -padx 2 -pady 2 -fill both -expand 1
  318.   pack .memoryViewer.pageDown -side left -padx 2 -pady 2 -fill both -expand 1
  319.   pack .memoryViewer.dismiss -side left -padx 2 -pady 2 -fill both -expand 1
  320.  
  321.   PutLine "ListMaximumAddress $MemoryViewer(AddressSpace)"
  322.   set MemoryViewer(MaximumAddress) [lindex [GetList] 0]
  323.   set MemoryViewer(AddressLength) [string length $MemoryViewer(MaximumAddress)]
  324.   scan $MemoryViewer(MaximumAddress) "%x" MemoryViewer(MaximumAddress)
  325.  
  326.   PutLine "ListGranularity"
  327.   set MemoryViewer(WordLength) [expr [lindex [GetList] 0]*2]
  328.  
  329.   ## Get the width and height of the text window
  330.   if {[scan [.memoryViewer.text configure -width] \
  331.             "-width width Width 80 %d" width] != "1"} {
  332.     set width "55"
  333.   } 
  334.   if {[scan [.memoryViewer.text configure -height] \
  335.             "-height height Height 24 %d" height] != "1"} {
  336.     set height "10"
  337.   } 
  338.  
  339.   ## Calculate the number of words per line
  340.   set MemoryViewer(WPL) [expr ($width-1-$MemoryViewer(AddressLength))/(1+$MemoryViewer(WordLength))]
  341.  
  342.   ## Calculate the number of words per screen
  343.   set MemoryViewer(WPS) [expr ($height*$MemoryViewer(WPL))]
  344.  
  345.   RefreshMemoryViewer
  346. }
  347.  
  348.